package com.siping.并发.练习题;

import java.util.concurrent.Semaphore;

/**
 * @author Yang Xu
 * @date 2021/9/6 10:36
 * @description:
 */
public class SemaphoreTest {

    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(5);
        for (int i = 0; i < 10; i++) {
            Thread thread = new Thread(() -> {
                try {
                    semaphore.acquire();
                    System.out.println(("执行任务，" + Thread.currentThread().getName()));
                    Thread.sleep(5000);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                semaphore.release();
            }, "t" + i);
            thread.start();
        }
    }
}
